const app = getApp()
import {
  createOrder,
  wxPlay,
  PlayZero
} from "../../config/https";
//生成订单、微信支付、支付 0 元的接口
Page({

  /**
   * 页面的初始数据
   */
  data: {
    paymentType: '',        //1为订单2为保障金3为手续费
    orderInfo: ''           //订单信息
  },
  playMoney(){
    let order_no = this.data.orderInfo.order_no
    let totalprice = this.data.orderInfo.totalprice
    if(this.data.paymentType == 2){
      PlayZero({
        token: wx.getStorageSync('token'),
        order_no,
        price: totalprice
      }).then(res => {
        app.$tip(res.msg)
        if(res.code == 1){
          setTimeout(() => {
            wx.redirectTo({
              url: '/pages/paymentResult/paymentResult?type=1',
            })
          },1000)
        }else{
          setTimeout(() => {
            wx.redirectTo({
              url: '/pages/paymentResult/paymentResult?type=2',
            })
          },1000)
        }
      })
      return
    }
    wxPlay({
      token: wx.getStorageSync('token'),
      order_no: order_no
    }).then(res => {
      if(res.code == 0){
        app.$tip(res.msg)
        return
      }
      wx.requestPayment({
        timeStamp: res.data.timeStamp,
        nonceStr: res.data.nonceStr,
        package: res.data.package,
        signType: 'MD5',
        paySign: res.data.paySign,
        success (res) { 
          wx.redirectTo({
            url: '/pages/paymentResult/paymentResult?type=1',
          })
        },
        fail (res) {
          wx.redirectTo({
            url: '/pages/paymentResult/paymentResult?type=2',
          })
        }
      })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      paymentType: options.paymentType
    })
    createOrder({
      token: wx.getStorageSync('token'),
      order_id: options.orderid,
      state: this.data.paymentType
    }).then(res => {
      if(res.code == 1){
        this.setData({
          orderInfo: res.data
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})